//
//  RBCompeLiveListController.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/13.
//

#import "RBCompeLiveListController.h"
#import "RBCompeFootBallLiveView.h"
#import "RBCompeBasBallLiveView.h"
#import "RBCompeLiveListCell.h"
#import "RBCompeLiveTextModel.h"
#import "RBCompeBasBallModel.h"
#import "RBCompeLiveHeaderView.h"

@interface RBCompeLiveListController ()
@property(assign,nonatomic)NSInteger pageNum;
@property(strong,nonatomic)NSMutableArray *dataArray;
@property(strong,nonatomic)NSMutableArray *statsArray;
@property(strong,nonatomic)NSMutableArray *statsPerArray;
@property(strong,nonatomic)RBCompeFootBallLiveView *compeFootBallLiveView;
@property(strong,nonatomic)RBCompeBasBallLiveView *compeBasBallLiveView;
@property(strong,nonatomic)RBEmptyView *emptyShowView;
@end

@implementation RBCompeLiveListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.view.backgroundColor = RBColor(245, 245, 245);
    
    [self rb_initTableViewUI];
    [self rb_initRefresh];
}
-(void)rb_initTableViewUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.aimView = self.tableView;
    self.aimView.mj_footer.hidden = YES;
    [self.tableView registerClass:[RBCompeLiveListCell class] forCellReuseIdentifier:@"RBCompeLiveListCell"];
    
    if ([self.sportId intValue]==1) {
        self.tableView.tableHeaderView = self.compeFootBallLiveView;
    }else{
        self.tableView.tableHeaderView = self.compeBasBallLiveView;
    }
}
-(void)rb_initRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(rb_loadQueryLiveData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
}
-(void)rb_loadQueryLiveData{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    dict[@"sportId"] = self.sportId;
    dict[@"matchId"] = self.matchId;
    MJWeakSelf
    [RBNetworkTool GET:RBQueryLiveURL parameters:dict success:^(id responseObject) {
        if ([responseObject[@"success"] boolValue]) {
            
            [weakSelf.dataArray removeAllObjects];
            [weakSelf.statsArray removeAllObjects];
            [weakSelf.statsPerArray removeAllObjects];
            
            if ([weakSelf.sportId intValue]==1) {
                NSArray *liveArray = [RBCompeLiveTextModel mj_objectArrayWithKeyValuesArray:[RBSafeString(responseObject[@"result"], @"tlive") mj_JSONObject]];
                [weakSelf.dataArray addObjectsFromArray:liveArray];
                //数组反转
                weakSelf.dataArray = (NSMutableArray *)[[weakSelf.dataArray reverseObjectEnumerator] allObjects].mutableCopy;
                
                NSMutableArray *statsArray = [RBSafeString(responseObject[@"result"], @"stats") mj_JSONObject];
                for (NSDictionary *dic in statsArray) {
                    RBCompeLiveStatsModel *model = [[RBCompeLiveStatsModel alloc]init];
                    model.type = [NSString stringWithFormat:@"%@",dic[@"type"]];
                    model.home = [NSString stringWithFormat:@"%@",dic[@"home"]];
                    model.away = [NSString stringWithFormat:@"%@",dic[@"away"]];
                    [weakSelf.statsArray addObject:model];
                }
                weakSelf.compeFootBallLiveView.homeNameZh = weakSelf.homeNameZh;
                weakSelf.compeFootBallLiveView.awayNameZh = weakSelf.awayNameZh;
                weakSelf.compeFootBallLiveView.statusArray = weakSelf.statsArray;
            }else{
                NSMutableArray *statsArray = [RBSafeString(responseObject[@"result"], @"stats") mj_JSONObject];
                NSMutableArray *statsPerArray = [RBSafeString(responseObject[@"result"], @"score") mj_JSONObject];
                for (NSArray *array in statsArray) {
                    if (array.count == 3) {
                        RBCompeLiveStatsModel *model = [[RBCompeLiveStatsModel alloc] init];
                        model.type = [NSString stringWithFormat:@"%@",array[0]];
                        model.home = [NSString stringWithFormat:@"%@",array[1]];
                        model.away = [NSString stringWithFormat:@"%@",array[2]];
                        [weakSelf.statsArray addObject:model];
                    }
                }
                if (statsPerArray.count == 5) {
                    NSArray *homeArr = statsPerArray[3];
                    NSArray *awyArr = statsPerArray[4];
                    if (homeArr.count == 5 && awyArr.count == 5) {
                        for (int i=0;i<homeArr.count;i++) {
                            RBCompeLiveStatsModel *model = [[RBCompeLiveStatsModel alloc] init];
                            model.type = [NSString stringWithFormat:@"%d",i+100];
                            model.home = [NSString stringWithFormat:@"%@",homeArr[i]];
                            model.away = [NSString stringWithFormat:@"%@",awyArr[i]];
                            [weakSelf.statsArray addObject:model];
                        }
                    }
                }
                NSArray *liveArray = [RBSafeString(responseObject[@"result"], @"tlive") mj_JSONObject];
                for (int i = 0; i<liveArray.count; i++) {
                    NSMutableArray *titleArr = liveArray[i];
                    for (NSString *strTilte in titleArr) {
                        NSString  *midestrTilte = [NSString stringWithFormat:@"$第%d$%@",i,strTilte];
                        RBCompeBasBallModel *compeBasBallModel = [[RBCompeBasBallModel alloc] init];
                        compeBasBallModel.midestrTilte = midestrTilte;
                        [weakSelf.dataArray addObject:compeBasBallModel];
                    }
                }
                //数组反转
                weakSelf.dataArray = (NSMutableArray *)[[weakSelf.dataArray reverseObjectEnumerator] allObjects].mutableCopy;
                weakSelf.compeBasBallLiveView.homeNameZh = weakSelf.awayNameZh;
                weakSelf.compeBasBallLiveView.awayNameZh = weakSelf.homeNameZh;
                weakSelf.compeBasBallLiveView.statusArray = weakSelf.statsArray;
            }
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }
    } error:^(id responseObject) {
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    self.tableView.mj_footer.hidden = (self.dataArray.count == 0);
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RBCompeLiveListCell *compeLiveListCell = [tableView dequeueReusableCellWithIdentifier:@"RBCompeLiveListCell" forIndexPath:indexPath];
    if ([self.sportId integerValue] == 1) {//足球
        compeLiveListCell.homeName = self.awayNameZh;
        compeLiveListCell.awayName = self.homeNameZh;
        compeLiveListCell.model = self.dataArray[indexPath.row];
    }else{
        compeLiveListCell.homeName = self.homeNameZh;
        compeLiveListCell.awayName = self.awayNameZh;
        compeLiveListCell.basModel = self.dataArray[indexPath.row];
    }
    if (indexPath.row == 0) {
        compeLiveListCell.topLineView.hidden = YES;
        compeLiveListCell.bottomLineView.hidden = NO;
    }else if(indexPath.row == self.dataArray.count - 1){
        compeLiveListCell.topLineView.hidden = NO;
        compeLiveListCell.bottomLineView.hidden = YES;
    }else{
        compeLiveListCell.topLineView.hidden = NO;
        compeLiveListCell.bottomLineView.hidden = NO;
    }
    return compeLiveListCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.sportId integerValue] == 1) {//足球
        RBCompeLiveTextModel *model = self.dataArray[indexPath.row];
        return model.cellHeight;
    }else{
        RBCompeBasBallModel *model = self.dataArray[indexPath.row];
        return model.basCellHeight;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    RBCompeLiveHeaderView *compeLiveHeaderView = [[RBCompeLiveHeaderView alloc] initWithFrame:CGRectMake(0, 0, RBScreenWidth, RBScaleWidth(65))];
    return compeLiveHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return RBScaleWidth(65);
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (!RBArrayIsEmpty(self.dataArray)) {
        self.emptyShowView.hidden = YES;
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RBScreenWidth, RBScaleWidth(15))];
        footerView.backgroundColor = [UIColor whiteColor];
        return footerView;
    }
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RBScreenWidth, RBScaleWidth(300))];
    footerView.backgroundColor = [UIColor whiteColor];
    [self.emptyShowView rb_showEmptyWithBaseView:footerView OriginY:0 ImageName:@"qsy_zwxl" Title:@"暂无数据" isShowLoadBtn:NO];
    MJWeakSelf
    self.emptyShowView.reloadButtonCallbackBlock = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    self.emptyShowView.hidden = NO;
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (!RBArrayIsEmpty(self.dataArray)) {
        return RBScaleWidth(15);
    }else{
        return RBScaleWidth(300);
    }
}
- (NSMutableArray *)dataArray{
    if(_dataArray==nil){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)statsArray{
    if(_statsArray==nil){
        _statsArray = [NSMutableArray array];
    }
    return _statsArray;
}
- (NSMutableArray *)statsPerArray{
    if(_statsPerArray==nil){
        _statsPerArray = [NSMutableArray array];
    }
    return _statsPerArray;
}
-(RBCompeFootBallLiveView *)compeFootBallLiveView{
    if(_compeFootBallLiveView==nil){
        _compeFootBallLiveView = [[RBCompeFootBallLiveView alloc] init];
        _compeFootBallLiveView.backgroundColor = [UIColor whiteColor];
        _compeFootBallLiveView.frame = CGRectMake(0, 0, RBScreenWidth, RBScaleWidth(305));
    }
    return _compeFootBallLiveView;
}
-(RBCompeBasBallLiveView *)compeBasBallLiveView{
    if(_compeBasBallLiveView==nil){
        _compeBasBallLiveView = [[RBCompeBasBallLiveView alloc] init];
        _compeBasBallLiveView.backgroundColor = [UIColor whiteColor];
        _compeBasBallLiveView.frame = CGRectMake(0, 0, RBScreenWidth, RBScaleWidth(426));
    }
    return _compeBasBallLiveView;
}
- (RBEmptyView *)emptyShowView{
    if (_emptyShowView==nil) {
        _emptyShowView = [[RBEmptyView alloc] init];
        _emptyShowView.hidden = YES;
    }
    return _emptyShowView;
}

@end
